package com.sskj.b70.v.activity;

import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.TabItem;
import com.sskj.b70.m.bean.TradeInfoBean;
import com.sskj.b70.p.activity.TradePresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.DisposUtil;
import com.sskj.b70.utils.NumberUtil;
import com.sskj.b70.v.base.BaseActivity;
import com.sskj.b70.v.fragment.NewTradePositionFragment;
import com.sskj.b70.v.fragment.TradeDealFragment;
import com.sskj.b70.v.fragment.TradeDeputeFragment;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * 交易订单界面
 * <pre>
 *     author : 吕志豪
 *     e-mail : 1030753080@qq.com
 *     time   : 2018/04/04
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class TradeActivity extends BaseActivity<TradePresenter> {
    @BindView(R.id.slidingTabLayout)
    CommonTabLayout slidingTabLayout;
    @BindView(R.id.tv_trade_id)
    TextView tvTradeId;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_right)
    ImageView ivRight;

    private ArrayList<Fragment> mFragments;
    private Disposable subscribe;


    @Override
    protected void onDestroy() {
//        mPresenter.cancel();
        DisposUtil.close(subscribe);
        super.onDestroy();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_trade;
    }

    @Override
    public TradePresenter getPresenter() {
        return new TradePresenter();
    }

    @Override
    protected void onViewBinding() {
        ClickUtil.click(ivBack, this::onBackPressed);
        ivRight.setVisibility(View.GONE);

        tvTradeId.setText(App.INSTANCE.getString(R.string.strTradeActivity0));
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.strTradeActivity1), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.strTradeActivity2), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.strTradeActivity3), 0, 0));
        mFragments = new ArrayList<>();
        mFragments.add(new NewTradePositionFragment());
        mFragments.add(new TradeDeputeFragment());
        mFragments.add(new TradeDealFragment());
        slidingTabLayout.setTabData(tabItems, this, R.id.trade_content, mFragments);
        startTimer();
    }

    private void startTimer() {
        subscribe = Flowable.interval(0, 1, TimeUnit.SECONDS)
                .onBackpressureDrop()
                .subscribe(i -> mPresenter.getCapital());
    }

    private void setItem(int itemId, String title, String content) {
        setItem(itemId, title, content, false);
    }

    private void setItem(int itemId, String title, String content, boolean isRed) {
        ((TextView) findViewById(itemId).findViewById(R.id.tv_title)).setText(title);
        ((TextView) findViewById(itemId).findViewById(R.id.tv_content)).setText(content);
        if (isRed) {
            ((TextView) findViewById(itemId).findViewById(R.id.tv_title)).setTextColor(ContextCompat.getColor(this, R.color.red_eb));
            ((TextView) findViewById(itemId).findViewById(R.id.tv_content)).setTextColor(ContextCompat.getColor(this, R.color.red_eb));
        }
    }

    public void updateTrade(TradeInfoBean bean) {
        setItem(R.id.trade_item_dtqy, App.INSTANCE.getString(R.string.strTradeActivity4), NumberUtil.keepMax4(bean.getAllFound() + ""));
        setItem(R.id.trade_item_kyzj, App.INSTANCE.getString(R.string.strTradeActivity6), NumberUtil.keepMax4(bean.getUsableFund() + ""));
        setItem(R.id.trade_item_djbzj, App.INSTANCE.getString(R.string.strTradeActivity8), NumberUtil.keepMax4(bean.getHasUsedFund() + ""));
        setItem(R.id.trade_item_fdyk, App.INSTANCE.getString(R.string.strTradeActivity10), NumberUtil.keepMax4(bean.getFloatWinLoseFee() + ""));
        setItem(R.id.trade_item_pcyk, App.INSTANCE.getString(R.string.strTradeActivity12), NumberUtil.keepMax4(bean.getCloseWinLoseFee() + ""));
        setItem(R.id.trade_item_fxl, App.INSTANCE.getString(R.string.strTradeActivity14), bean.getProportion() + "%", true);
        ((NewTradePositionFragment) mFragments.get(0)).update();
    }

}
